﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Common.StringExtensions
{
    /// <summary>
    /// 字符串扩展方法
    /// </summary>
    public static class StringExtensionFunctions
    {
        /// <summary>
        /// 确保字符串不超过指定长度（超过后进行截取）
        /// </summary>
        /// <param name="s">原字符串</param>
        /// <param name="maxinum">最大长度</param>
        /// <returns></returns>
        public static string EnsureMaxLength(this string s, int maxinum)
        {
            if (string.IsNullOrEmpty(s))
            {
                return s;
            }
            return s.Length > maxinum ? s.Substring(0, maxinum) : s;
        }
        /// <summary>
        /// 如果字符串为empty，则返回null
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string NullIfEmpty(this string s)
        {
            return string.IsNullOrEmpty(s) ? null : s;
        }
        /// <summary>
        /// 判断字符串是否为空或null
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsNullOrEmpty(this string s)
        {
            return string.IsNullOrEmpty(s);
        }
        /// <summary>
        /// 判断字符串是否为空或null，或纯空格
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsNullOrWhiteSpace(this string s)
        {
            return string.IsNullOrWhiteSpace(s);
        }
        /// <summary>
        /// 判断两个字符串是否相同，忽略大小写差异
        /// </summary>
        /// <param name="left"></param>
        /// <param name="right"></param>
        /// <returns></returns>
        public static bool EqualsIgnoreCase(this string left , string right)
        {
            return string.Equals(left, right, StringComparison.OrdinalIgnoreCase);
        }
    }
}
